package com.SMP.service.class_room;

import com.SMP.dao.CourseMapper;
import com.SMP.dao.GradeclassMapper;
import com.SMP.model.Course;
import com.SMP.model.CourseExcel;
import com.SMP.model.ExcelBean;
import com.SMP.utils.ExcelUtil;
import com.SMP.utils.Utils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.*;

@Service
public class CourseService {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    GradeclassMapper gradeclassMapper;

    /**
     * 增加课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    public int addCourse(Course course){
        int result=0;
        course.setId(Utils.getsUUID());
        course.setStandby1("1");
        result=courseMapper.insert(course);
        return result;
    }

    /**
    * @Description: 主键查询课程
    * @Param: [id]
    * @return: com.SMP.model.Course
    * @Author: WYHY
    * @Date: 2019/2/25
    */
    public Course selCourseById(String id){
        return courseMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    public int updCourse(Course course){
        int result=0;
        result=courseMapper.updateByPrimaryKeySelective(course);
        return result;
    }

    /**
     * 条件查询课程
     * @author WYHY
     * @param course 课程对象
     *
     * */
    public List<Course> selCourse(Course course){
        course.setStandby1("1");
        List<Course> result=courseMapper.selectByCourse(course);
        return result;
    }

    /**
     * 删除整张课程表
     * @author WYHY
     * @param grade,classs 年级，班级
     *
     * */
    public int deleteByGC(String claid,String grade,String classs){
        int result=0;
        Course course=new Course();
        course.setGrade(grade);
        course.setClasss(classs);
        int is=selCourse(course).size();
        if(is==0){
            gradeclassMapper.deleteByPrimaryKey(claid);
            result=-1;
        }else{
            result=courseMapper.deleteByGC(grade,classs);
        }
        return result;
    }

    /**
     * 删除一节课程
     * @author WYHY
     * @param id id
     *
     * */
    public int deleteByCourse(String id){
        int result=courseMapper.deleteByPrimaryKey(id);
        return result;
    }

    /**
     * 导出Excel
     *
     * @throws Exception
     */

//    public XSSFWorkbook exportExcelInfo(String studentData,Course course) throws Exception {
//        String dir = request.getSession().getServletContext().getRealPath("/");// 总路径
//        String ticketPath = "Ticket/";
//        String ticketFilePath = dir + ticketPath;
//        // 根据条件查询数据，把数据装载到一个list中
//        List<Course> list = courseMapper.selectByCourse(course);
//        List<CourseExcel> lists = new ArrayList<CourseExcel>();
//        for (int i = 0; i < list.size(); i++) {
//            Course courses = list.get(i);
//            Map map=new HashMap();
//            map.put("lessonsId",courses.getLessonsId());
//            map.put("lessonsNo",courses.getLessonsNo());
//            map.put("week",courses.getWeek());
//        }
//        Template t=null;
//        try {
//            // test.ftl为要装载的模板
//            t = configuration.getTemplate("CourseExcel.ftl");
//            t.setEncoding("utf-8");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        File ticketFile = new File(ticketFilePath);
//        if (!ticketFile.exists()) {
//            ticketFile.mkdirs();
//        }
//        // 输出文档路径及名称
//        File outFile = new File(ticketFilePath, examinationtable.getEamineeId() + ".doc");
//        Writer out = null;
//        try {
//            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        try {
//            t.process(map, out);
//            out.close();
//        } catch (TemplateException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return null;
//    }
}
